<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/22
 * Time: 14:28
 */

namespace App\Services;

use App\Models\Media;
use App\Models\MediaRecommend;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class MediaService
{

    /*
     * 推荐视频
     * */
    public function channge_media_recommend($params)
    {
        $media_id = $params['media_id'];
        $sort = IT('sort',0);
        $status = IT('status',1);

        DB::beginTransaction();
        try{
            $media_rows = Media::where([['id', '=', $media_id], ['is_delete', '=', 0]])->first();
            if(empty($media_rows))
                throw new \Exception("选择的视频不存在");
            if($media_rows->status != '1')
                throw new \Exception("视频未启用");
            if($media_rows->is_private != '0')
                throw new \Exception("该视频不是公开视频");

            $media_recommend_model = MediaRecommend::where([['media_id', '=', $media_id]])->first();
            if(empty($media_recommend_model)){
                $media_recommend_model = new MediaRecommend();
                $media_recommend_model->create_time = time();
            }
            $media_recommend_model->media_id = $media_id;
            $media_recommend_model->media_uid = $media_rows->uid;
            $media_recommend_model->status = $status;
            $media_recommend_model->sort = $sort;
            $media_recommend_model->update_time = time();
            if(!$media_recommend_model->save())
                throw new \Exception("操作失败");

            $remmond_sort = self::media_remmond_sort($media_id, $sort);
            if($remmond_sort['status'] == 'error')
                throw new \Exception("排序异常");

            DB::commit();
            return ['status'=>'success', 'message'=>'修改成功', 'data'=>[]];
        } catch (\Exception $e) {
            DB::rollBack();
            return ['status'=>'error', 'message'=>$e->getMessage(), 'data'=>[]];
        }

        return json_success('ok');
    }

    /*
     * 视频排序
     * 指定的media_id 对应的排序值
     *
     * */
    public function media_remmond_sort($media_id, $sort)
    {
        try{
            $list = MediaRecommend::select(['id', 'sort', 'media_id'])->where([['status', '=', 1]])->orderby('sort', 'asc')->get();

            if(!empty($list)){
                $temp_arr = [];
                foreach ($list as $key=>$value) {
                    if($value->media_id == $media_id)
                        continue;
                    $temp_arr[$value->sort]['media_id'] = $value->media_id;
                    $temp_arr[$value->sort]['sort'] = $value->sort;
                }

                $location = $sort > 0 ? ($sort-1): 99999;
                array_splice($temp_arr, $location, 0, [['media_id'=>$media_id, 'sort'=>$sort]]); // 插入到位置3且删除0个
                $i=0;
                foreach ($temp_arr as $k=>$val){
                    $i++;
                    $update = MediaRecommend::where([['media_id', '=', $val['media_id']]])->update(['sort'=>$i]);
                    /*if(!$update)
                        throw new \Exception("排序失败");*/
                }
            }
            return ['status'=>'success', 'message'=>'排序成功', 'data'=>[]];
        }catch (\Exception $e) {
            return ['status'=>'error', 'message'=>$e->getMessage(), 'data'=>[]];
        }
    }

    /*
     * 视频排序
     * */
    public function media_sort()
    {
        $list = MediaRecommend::select(['id', 'sort', 'media_id'])->where([['status', '=', 1]])->orderby('sort', 'asc')->get();

        if(!empty($list)){
            $i=0;
            foreach ($list as $key=>$value) {
                $i++;
                $update = MediaRecommend::where([['id', '=', $value->id]])->update(['sort'=>$i]);
            }
        }
        return ['status'=>'success', 'message'=>'排序成功', 'data'=>[]];
    }

    /*
     * 删除推荐
     * */
    public function del_recommend($media_id)
    {
        try{
            $rows = MediaRecommend::where([['media_id', '=', $media_id]])->first();
            if(empty($rows))
                throw new \Exception("不存在推荐视频");
            $result = MediaRecommend::where([['id', '=', $rows->id]])->delete();
            if(!$result)
                throw new \Exception("删除失败");
            self::media_sort();
            return ['status'=>'error', 'message'=>'删除成功', 'data'=>[]];
        }catch (\Exception $e) {
            return ['status'=>'error', 'message'=>$e->getMessage(), 'data'=>[]];
        }
    }

}